import Input from "../../utils/Input";
import { FSMState } from "./lib/FSMState";
import Move from "./Move";

export default class PlayerWalkState extends FSMState {
    private _moveSpeed: number = 200;
    private _moveDir: cc.Vec2 = cc.Vec2.ZERO;

    private _move: Move = null;

    onEnter() {
        super.onEnter();
        this._move = this.fsm.node.getComponent(Move);
        this._move.speed = this._moveSpeed;
    }

    public update(dt: any): void {
        this._moveDir.set(cc.Vec2.ZERO);
        //垂直方向移动
        if (Input.getInstance().isKeyPressed(cc.macro.KEY.w) ||
            Input.getInstance().isKeyPressed(cc.macro.KEY.up)) {
            this._moveDir.y = 1;
        } else if (Input.getInstance().isKeyPressed(cc.macro.KEY.s) ||
            Input.getInstance().isKeyPressed(cc.macro.KEY.down)) {
            this._moveDir.y = -1;
        }

        if (Input.getInstance().isKeyPressed(cc.macro.KEY.d) ||
            Input.getInstance().isKeyPressed(cc.macro.KEY.right)) {
            this._moveDir.x = 1;
        } else if (Input.getInstance().isKeyPressed(cc.macro.KEY.a) ||
            Input.getInstance().isKeyPressed(cc.macro.KEY.left)) {
            this._moveDir.x = -1;
        }

        if (this._moveDir.equals(cc.Vec2.ZERO_R)) {
            this.fsm.data.setData("isKeyPressed", false);
            this._move.stop();
        } else {
            this._move.moveByDirection(this._moveDir);
        }
    }
}
